/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.main;

import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import mrusanov.towers.configuration.GameConfiguration;
import mrusanov.towers.configuration.GameConfigurationManager;
import mrusanov.towers.localization.Localization;
import mrusanov.towers.main.ui.EventQueueWithLoggedExceptions;
import mrusanov.towers.main.ui.MainForm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

	private static final Logger LOG = LoggerFactory.getLogger(Main.class);

	public static void main(String[] args) throws Exception {
		try {
			LOG.info("Application started. Trying to read configuration");
			GameConfiguration configuration = GameConfigurationManager.getConfiguration();
			Localization.setLang(configuration.getLanguage());

			EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
			queue.push(new EventQueueWithLoggedExceptions());

			useNimbusLookAndFeelIfPossible();

			EventQueue.invokeLater(new Runnable() {

				@Override
				public void run() {
					final DesktopGame game = new DesktopGame();
					MainForm form = new MainForm(game);
					game.setMainForm(form);
					form.setVisible(true);
				}
			});
		} catch (Exception ex) {
			LOG.error(ex.getMessage(), ex);
		}
	}

	private static void useNimbusLookAndFeelIfPossible() throws ClassNotFoundException, InstantiationException,
			IllegalAccessException, UnsupportedLookAndFeelException {
		LOG.info("Trying to use Nimbus look and feel");
		for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
			if ("Nimbus".equals(info.getName())) {
				UIManager.setLookAndFeel(info.getClassName());
				break;
			}
		}
	}

}
